#!/usr/bin/python
#encoding=UTF-8

import urllib,re,time,MySQLdb,sys,string

date = time.strftime('%Y-%m-%d')

#mysql>create datebase pub_sentiment
#mysql>grant all on pub_sentiment.* to 'pub_sentiment'@'localhost' IDENTIFIED BY '568147396';

try:
	con = MySQLdb.connect(host='localhost',user='pub_sentiment',passwd='568147396',db='pub_sentiment',charset='utf8')
except Exception, e:
	print e
	sys.exit()

cur = con.cursor()


url = 'http://news.cngold.org/index.html'
wp = urllib.urlopen(url)
result = wp.read()

start = result.find('热点排行')
end = result.find('民生经济')

result =result[start:end]

myUrl = re.findall('http.*html',result)
myKeyword = re.findall('title=".*\">',result)


#create table top10(date varchar(10),keyword binary(100),url char(200),attentionRate int);
for i in range(10):
    myKeyword[i] = myKeyword[i].replace(r'title="','').replace(r'">','')
    sWp = urllib.urlopen(myUrl[i])
    sResult = sWp.read()
    AttentionRate = re.search('\d{4}</a',sResult)
    att = string.atoi(AttentionRate.group(0).replace('</a',''))
    sql = "insert into top10(date,keyword,url,attentionRate) values('%s','%s','%s','%d');"%(date,myKeyword[i],myUrl[i],att)
    try:
	cur.execute(sql)
#	print sql
    except Exception,e:
	print e
con.commit()
cur.close()
con.close()